home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / System 7 / ctc 1.5 / eol-convert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-04  |  2.0 KB  |  86 lines  |  [TEXT/KAHL]

  1. /* eol-convert.c
  2.  *
  3.  * converts the EOL in TEXT files to the chosen system
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "ctc.h"
  9.  
  10. #define STRING_TERMINATOR    ('\0')
  11. #define CR    ((unsigned char) 0x0d)
  12. #define LF    ((unsigned char) 0x0a)
  13.  
  14. int process (FILE *path, char *EOL, char *buffer)
  15. {
  16.     char *start, *ptr;
  17.  
  18.     start = buffer;
  19.     for (ptr = buffer; *ptr; )
  20.         switch (*ptr) {
  21.             default:
  22.                 ptr++;
  23.                 break;
  24.             case CR:    /* MAC or DOS end of line has been found */
  25.                 *ptr = STRING_TERMINATOR;
  26.                 fprintf (path, "%s%s", start, EOL);
  27.                 ptr++;
  28.                 if (*ptr == LF) ptr++;    /* skip the LF */
  29.                 start = ptr;
  30.                 break;
  31.             case LF:    /* UNIX end of line has been found */
  32.                 *ptr = STRING_TERMINATOR;
  33.                 fprintf (path, "%s%s", start, EOL);
  34.                 ptr++;
  35.                 start = ptr;
  36.                 break;
  37.         }
  38.     if (start < ptr)    /* write the last line, maybe an EOL */
  39.         fprintf (path, "%s", start);
  40.     return 0;
  41. }
  42.  
  43.  
  44. void ConvertFile (char *thefile, int EOLmode)
  45. {
  46.     FILE *path;
  47.     long int length, qty;
  48.     char *buffer, EOL[4];
  49.     unsigned int i, j, k;
  50.  
  51.     switch (EOLmode) {
  52.         case DOS:    strcpy (EOL, "\r\n\0"); break;
  53.         case MAC:    strcpy (EOL, "\r\0"); break;
  54.         case UNIX:    strcpy (EOL, "\n\0"); break;
  55.         default:    return;
  56.     }
  57.     path = fopen (thefile, "rb");
  58.         if (!path) return;        /* could not open the file for read */
  59.         /*
  60.          * find the length of the file 
  61.          * and allocate a buffer for its contents
  62.          */
  63.         fseek (path, (long int) 0, SEEK_END);
  64.         length = ftell (path);
  65.         fseek (path, (long int) 0, SEEK_SET);
  66.         buffer = (char *) malloc ((size_t) length+1);
  67.         if (!buffer) return;    /* not enough memory for the buffer */
  68.         buffer[length] = STRING_TERMINATOR;    /* just in case */
  69.         /*
  70.          * read the file into the buffer
  71.          */
  72.         qty = fread ( (void *) buffer, (size_t) length, (size_t) 1, path);
  73.     /*
  74.      * re-open the file for writing
  75.      */
  76.     freopen (thefile, "wb", path);
  77.         if (!path) return;        /* could not open the file for write */
  78.         fseek (path, (long int) 0, SEEK_SET);
  79.         /*
  80.          * do the conversion of the EOL characters
  81.          */
  82.         process (path, EOL, buffer);
  83.     fclose (path);
  84.     free (buffer);
  85. }
  86.